package com.cc.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.cc.mall.entity.ProductDetailEntity;
import com.cc.mall.entity.ProductPictureEntity;
import com.cc.mall.service.ProductDetailService;
import com.cc.mall.service.ProductPictureService;
import com.cc.mall.vo.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cc.mall.dao.ProductDao;
import com.cc.mall.entity.ProductEntity;
import com.cc.mall.service.ProductService;
import org.springframework.util.StringUtils;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {

    @Autowired
    private ThreadPoolExecutor executor;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ProductPictureService productPictureService;

    @Autowired
    private ProductDetailService productDetailService;


    @Override
    public Page<ProductEntity> pageWithCondition(Map<String, Object> params) {
        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
        String catId = (String) params.get("catId");
        String status = (String) params.get("status");
        String bigTitle = (String) params.get("bigTitle");
        if(!StringUtils.isEmpty(bigTitle)){
            queryWrapper.like("big_title",bigTitle);
        }
        if(!StringUtils.isEmpty(catId)){
            queryWrapper.eq("pcid",Long.parseLong(catId));
        }
        if(!StringUtils.isEmpty(status)){
            queryWrapper.eq("publish_status",Integer.parseInt(status));
        }
        Page<ProductEntity> page = new Page<>(Long.parseLong((String)params.get("page")),
                Long.parseLong((String)params.get("limit")));
        baseMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Override
    public void up(Long id) {
        // 改为上架状态
        ProductEntity productEntity = new ProductEntity();
        productEntity.setId(id);
        productEntity.setPublishStatus(1);
        baseMapper.updateById(productEntity);
        // todo 修改完之后重新上架，是否要修改redis中的购物车信息(感觉只能加个定时任务了)

    }

    @Override
    public Page<ProductDetailEntity> getProductDetailListById(Map<String,Object> params) {
        QueryWrapper<ProductDetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",params.get("pid"));
        String bigTitle = (String) params.get("bigTitle");
        if(!StringUtils.isEmpty(bigTitle)){
            queryWrapper.like("big_title",bigTitle);
        }
        Page<ProductDetailEntity> page = new Page<>(Long.parseLong((String)params.get("page")),
                Long.parseLong((String)params.get("limit")));
        productDetailService.page(page,queryWrapper);
        return page;
    }

    @Override
    public void removeByProductId(Long id) {
        // 先删除商品详情
        productDetailService.remove(new QueryWrapper<ProductDetailEntity>().eq("pid",id));
        // 再删除商品
        baseMapper.deleteById(id);
    }

    @Override
    public List<ProductVo> getProductListByCatId(Long catId) {
        List<ProductVo> collect = baseMapper.getProductListByCatId(catId);
        return collect;
    }

    @Override
    public void down(Long id) {
        ProductEntity productEntity = new ProductEntity();
        productEntity.setId(id);
        productEntity.setPublishStatus(2);
        // 改为下架状态
        baseMapper.updateById(productEntity);
    }

    @Override
    public ProductDetailVo getProductBySkuId(Long skuId,Long spuId) throws ExecutionException, InterruptedException {
        ProductDetailVo productDetailVo = new ProductDetailVo();
        ProductEntity productEntity = baseMapper.selectById(spuId);
        // 大小标题
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            productDetailVo.setBigTitle(productEntity.getBigTitle());
            productDetailVo.setSmallTitle(productEntity.getSmallTitle());
            productDetailVo.setId(skuId);
            productDetailVo.setPid(spuId);
        }, executor);
        // 价格
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            ProductDetailEntity byId = productDetailService.getById(skuId);
            productDetailVo.setProductPrice(byId.getProductPrice());
        }, executor);
        // 规格
        CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> {
            List<ProductDetailEntity> list = productDetailService.list(new QueryWrapper<ProductDetailEntity>().eq("pid", spuId));
            List<OtherSizeVo> collect = list.stream().map(m -> {
                OtherSizeVo otherSizeVo = new OtherSizeVo();
                otherSizeVo.setSkuId(m.getId());
                otherSizeVo.setSpuId(m.getPid());
                otherSizeVo.setProductSize(m.getProductSize());
                if(skuId.equals(m.getId())){
                    otherSizeVo.setCheck(true);
                }
                return otherSizeVo;
            }).collect(Collectors.toList());
            productDetailVo.setOtherSizes(collect);
        }, executor);
        // 轮播图
        CompletableFuture<Void> future4 = CompletableFuture.runAsync(() -> {
            List<String> bannerPic = productPictureService.list(new QueryWrapper<ProductPictureEntity>().eq("pdid", spuId)).stream()
                    .map(m -> {
                        return m.getPicture();
                    }).collect(Collectors.toList());
            if(!StringUtils.isEmpty(productEntity.getDefaultPicture())){
                bannerPic.add(productEntity.getDefaultPicture());
            }
            if(bannerPic != null && bannerPic.size()>0)
                productDetailVo.setBannerPic(bannerPic);
        }, executor);
        CompletableFuture.allOf(future1,future2,future3,future4).get();
        return productDetailVo;
    }

    @Override
    public List<ProductDetail> getProductDetailBySpuId(Long spuId) {
        return baseMapper.getProductDetailBySpuId(spuId);
    }

    @Override
    public List<IdAndNumVo> getSpuIdAndNumList() {
        return baseMapper.getSpuIdAndNumList();
    }

    @Override
    public ProductEntity getByIdP(Long id) {
        return baseMapper.getByIdP(id);
    }
}